package com.ohuang.jiaxiaotong.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.ohuang.jiaxiaotong.R;
import com.ohuang.jiaxiaotong.entity.ClassUrl;
import com.shuyu.gsyvideoplayer.video.ListGSYVideoPlayer;

import java.util.ArrayList;

public class DataFileRvAdapter2 extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private ArrayList<ClassUrl> mData;

    public DataFileRvAdapter2(Context mContext, ArrayList<ClassUrl> mData) {
        this.mLayoutInflater = LayoutInflater.from(mContext);
        this.mContext = mContext;
        this.mData = mData;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 1) {
            return new DataFileRvAdapter2.MyVideoHolder
                    (mLayoutInflater.inflate(R.layout.rv_data_video_item, parent, false));
        } else if (viewType == 2) {
            return new DataFileRvAdapter2.MyImageHolder
                    (mLayoutInflater.inflate(R.layout.rv_data_img_item, parent, false));
        } else {
            return new DataFileRvAdapter2.MyTxtHolder
                    (mLayoutInflater.inflate(R.layout.rv_data_txt_item, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof DataFileRvAdapter2.MyImageHolder) {
            initMyImageHolder(holder, position);
        }
        if (holder instanceof DataFileRvAdapter2.MyVideoHolder) {
            initMyVideoHolder(holder, position);
        }
        if (holder instanceof DataFileRvAdapter2.MyTxtHolder) {
            initMyTXTHolder(holder, position);
        }


    }

    private void initMyVideoHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ((MyVideoHolder) holder).textView_username.setText(mData.get(position).getUsername());
        ((MyVideoHolder) holder).textView_content.setText(mData.get(position).getDataContent());
        ListGSYVideoPlayer listGSYVideoPlayer = ((MyVideoHolder) holder).listGSYVideoPlayer;
        initListGsyVideo(listGSYVideoPlayer, mData.get(position).getUrl());//初始化视频
        Glide.with(mContext).load(mData.get(position).getIconUrl())
                .error(R.drawable.img_icon)
                .fallback(R.drawable.img_icon)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(((MyVideoHolder) holder).imageView_icon);
        if (mData.get(position).getStatus() == 1) {
            ((MyVideoHolder) holder).checkBox_collect.setChecked(true);
        }
        ((MyVideoHolder) holder).textView_delete.setVisibility(View.GONE);
        if (onItemClickListener != null) {
            ((MyVideoHolder) holder).checkBox_collect
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            onItemClickListener.CheckBoxOnCheckedChanged(((MyVideoHolder) holder).checkBox_collect, position, buttonView, isChecked);
                        }
                    });
        }
    }

    private void initListGsyVideo(ListGSYVideoPlayer listGSYVideoPlayer, String url) {
        listGSYVideoPlayer.setUp(url, true, null, null, "");
        listGSYVideoPlayer.getTitleTextView().setVisibility(View.GONE);
        listGSYVideoPlayer.getBackButton().setVisibility(View.GONE);

    }

    //显示图片布局
    private void initMyImageHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ((MyImageHolder) holder).textView_username.setText(mData.get(position).getUsername());
        ((MyImageHolder) holder).textView_content.setText(mData.get(position).getDataContent());
        Glide.with(mContext).load(mData.get(position).getUrl())
                .placeholder(R.drawable.dra_imgloading)//设置加载中图片
                .into(((MyImageHolder) holder).imageView_img);
        Glide.with(mContext).load(mData.get(position).getIconUrl())
                .error(R.drawable.img_icon)
                .fallback(R.drawable.img_icon)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(((MyImageHolder) holder).imageView_icon);
        if (mData.get(position).getStatus() == 1) {
            ((MyImageHolder) holder).checkBox_collect.setChecked(true);
        }
        ((MyImageHolder) holder).textView_delete.setVisibility(View.GONE);
        if (onItemClickListener != null) {
            ((MyImageHolder) holder).checkBox_collect
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            onItemClickListener.CheckBoxOnCheckedChanged(((MyImageHolder) holder).checkBox_collect, position, buttonView, isChecked);
                        }
                    });
        }
    }

    //显示TXT布局
    private void initMyTXTHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ((MyTxtHolder) holder).textView_username.setText(mData.get(position).getUsername());
        ((MyTxtHolder) holder).textView_content.setText(mData.get(position).getDataContent());

        Glide.with(mContext).load(mData.get(position).getIconUrl())
                .error(R.drawable.img_icon)
                .fallback(R.drawable.img_icon)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(((MyTxtHolder) holder).imageView_icon);
        if (mData.get(position).getStatus() == 1) {
            ((MyTxtHolder) holder).checkBox_collect.setChecked(true);
        }
        ((MyTxtHolder) holder).textView_delete.setVisibility(View.GONE);
        if (onItemClickListener != null) {
            ((MyTxtHolder) holder).checkBox_collect
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            onItemClickListener.CheckBoxOnCheckedChanged(((MyTxtHolder) holder).checkBox_collect, position, buttonView, isChecked);
                        }
                    });
        }
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.onItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
        //收藏按钮监听
        void CheckBoxOnCheckedChanged(CheckBox checkBox, int position, CompoundButton buttonView, boolean isChecked);

        //删除按钮监听
//        void DeleteOnClick(RecyclerView.ViewHolder holder, int position);
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    class MyVideoHolder extends RecyclerView.ViewHolder {
        CheckBox checkBox_collect;
        TextView textView_username, textView_content, textView_delete;
        ImageView imageView_icon;
        ListGSYVideoPlayer listGSYVideoPlayer;

        public MyVideoHolder(@NonNull View itemView) {
            super(itemView);
            checkBox_collect = itemView.findViewById(R.id.checkbox_collect_video_data_rv);
            textView_username = itemView.findViewById(R.id.txt_username_video_data_rv);
            textView_content = itemView.findViewById(R.id.txt_content_video_data_rv);
            textView_delete = itemView.findViewById(R.id.txt_delete_video_data_rv);
            imageView_icon = itemView.findViewById(R.id.img_icon_video_data_rv);
            listGSYVideoPlayer = itemView.findViewById(R.id.GsyVideo_data_rv);
        }
    }

    class MyImageHolder extends RecyclerView.ViewHolder {
        CheckBox checkBox_collect;
        TextView textView_username, textView_content, textView_delete;
        ImageView imageView_icon, imageView_img;

        MyImageHolder(View itemView) {
            super(itemView);
            checkBox_collect = itemView.findViewById(R.id.checkbox_collect_img_data_rv);
            textView_username = itemView.findViewById(R.id.txt_username_img_data_rv);
            textView_content = itemView.findViewById(R.id.txt_content_img_data_rv);
            textView_delete = itemView.findViewById(R.id.txt_delete_img_data_rv);
            imageView_icon = itemView.findViewById(R.id.img_icon_img_data_rv);
            imageView_img = itemView.findViewById(R.id.img_data_rv);

        }
    }

    class MyTxtHolder extends RecyclerView.ViewHolder {
        CheckBox checkBox_collect;
        TextView textView_username, textView_content, textView_delete;
        ImageView imageView_icon;

        public MyTxtHolder(@NonNull View itemView) {
            super(itemView);
            checkBox_collect = itemView.findViewById(R.id.checkbox_collect_txt_data_rv);
            textView_username = itemView.findViewById(R.id.txt_username_txt_data_rv);
            textView_content = itemView.findViewById(R.id.txt_content_txt_data_rv);
            textView_delete = itemView.findViewById(R.id.txt_delete_txt_data_rv);
            imageView_icon = itemView.findViewById(R.id.img_icon_txt_data_rv);

        }
    }

    public int getItemViewType(int position) {
        if ("mp4".equals(mData.get(position).getSuffixName())) {
            return 1;
        } else if ("png".equals(mData.get(position).getSuffixName())) {
            return 2;
        } else {
            return 3;
        }

    }
}